// pages/xiaoshuo/xiaoshuo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,
    searchNum: 1,
    allKindsData: [],
    allData: [],
    currentData: [],
    activeIndex: 0,
    isShowMenu: false,
    tagName: '全部',
    inputValue: '',
    flag: true,
    hisflag: false,
    resultflag: false,
    searchResList: [],
    btnClick: false,
    isFocus: false

  },
  hisTap() {
    if (!this.data.inputValue) {
      console.log("=====================");
      this.setData({
        flag: true,
        hisflag: false,
        resultflag: false,
        isFocus: false

      })

    }
  },
  onFocus() {
    console.log("聚焦了");

    this.setData({
      flag: false,
      hisflag: true,
      resultflag: false,
    });
    if (!this.data.inputValue) {
      this.setData({
        isFocus: true
      });

    }

  },
  onBlur() {
    console.log("失去焦了");
    // // // 搜索框没有内容并且 显示搜索历史时, 并且没有点击按钮 失焦 显示原始页面
    // if (!this.data.inputValue && this.data.hisflag && !this.data.btnClick) {
    //   console.log("jinlai");
    //   this.setData({
    //     flag: true,
    //     hisflag: false,
    //     resultflag: false

    //   })


    // }
    // if(!this.data.inputValue && this.data.hisflag){
    //   this.setData({
    //     flag:true,
    //     hisflag:false,
    //     resultflag:false,
    //   })

    // }

    // if(this.data.hisflag){
    //   if(!this.data.inputValue){
    //     this.setData({
    //       flag:true,
    //       hisflag:false,
    //       resultflag:false,
    //     })

    //   }

    // }
  },
  onClear() {
    this.setData({
      searchResList: []
    })

  },
  onHisResTab(e) {
    console.log("dainji 按钮");
    let word = e.currentTarget.dataset.word;
    let arr = this.data.searchResList.filter((o) => o != word);
    arr.unshift(word),
      this.setData({
        searchResList: arr,
        inputValue: word,
        flag: false,
        hisflag: false,
        resultflag: true,
        btnClick: true

      })
    console.log(this.data.inputValue);
    wx.request({
      url: 'https://apis.netstart.cn/bcomic/Search',
      data: {
        'styleId': -1,
        'areaId': -1,
        'isFinish': -1,
        'order': -1,
        'pageNum': 1,
        'pageSize': 20,
        'isFree': -1,
        'keyWord': word
      },
      success: (res) => {
        this.setData({
          resList: res.data.data.list
        })

      }
    })


  },
  onquxiao() {
    this.setData({
      flag: true,
      hisflag: false,
      resultflag: false,
      inputValue:'',
      isFocus: false


    })


  },

  onSearch() {
    wx.request({
      url: 'https://apis.netstart.cn/bcomic/Search',
      data: {
        'styleId': -1,
        'areaId': -1,
        'isFinish': -1,
        'order': -1,
        'pageNum': 1,
        'pageSize': 20,
        'isFree': -1,
        'keyWord': this.data.inputValue
      },
      success: (res) => {
        let arr = this.data.searchResList;
        arr.unshift(this.data.inputValue),
          this.setData({
            resList: res.data.data.list,
            searchResList: arr,
            flag: false,
            hisflag: false,
            resultflag: true,

          })

      }
    })

  },
  onChange(e) {
    this.setData({
      inputValue: e.detail,
      isFocus: false
    });

  },

  onCancel(e) {

    console.log("e", e);
    this.setData({
      inputValue: '',
      flag: true,
      hisflag: false,
      resultflag: false,

    });

  },
  todet(e) {
    let id = e.currentTarget.dataset.id
    wx.request({
      url: 'https://apis.netstart.cn/bcomic/ComicDetail',
      data: {
        'comicId': id
      },
      success: (res) => {
        if (res.statusCode == 200) {
          console.log(res);
          let dt = res.data.data;
          const data = JSON.stringify(dt)
          this.pageRouter.navigateTo({
            url: '../readintro/readintro?data=' + encodeURIComponent(data),
          });

        }

      }
    })
  },
  kindClick() {
    this.setData({
      isShowMenu: !this.data.isShowMenu
    });
    console.log(this.data.isShowMenu);

  },
  ontap(e) {
    let id = e.currentTarget.dataset.id;
    let obj = this.data.allKindsData.filter((o) => {
      return o.id === id
    });
    this.setData({
      currentData: obj[0].data,
      activeIndex: e.currentTarget.dataset.index + 1,
      tagName: e.currentTarget.dataset.name
    });
  },
  ontapall(e) {
    this.setData({
      currentData: this.data.allData,
      activeIndex: 0,
      tagName: "全部"
    });
  },
  // 点击轮播图跳转
  todetail(e) {
    if (e.currentTarget.dataset.type === 2) {
      let id = e.currentTarget.dataset.id;
      console.log("w jinlail");
      wx.request({
        url: 'https://apis.netstart.cn/bcomic/ComicDetail',
        data: {
          'comicId': id
        },
        success: (res) => {
          console.log(res);
          let dt = res.data.data;
          const data = JSON.stringify(dt)
          this.pageRouter.navigateTo({
            url: '../../pages/readintro/readintro?data=' + encodeURIComponent(data),
          });
        }
      })

    } else if (e.currentTarget.dataset.type === 3) {
      // outerChain为url链接
      let outerChain = e.currentTarget.dataset.value;
      console.log(outerChain);
      wx.navigateTo({
        url: '../../pages/outer/outer?src=' + encodeURIComponent(outerChain),
      })
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //请求初始页面渲染的数据
    wx.request({
      url: 'https://apis.netstart.cn/bcomic/ClassPage',
      success: (res) => {
        this.setData({
          currentData: res.data.data,
          allData: res.data.data
        })
        // console.log(res.data);

      }
    })

    // 分类
    wx.request({
      url: 'https://apis.netstart.cn/bcomic/AllLabel',
      success: (res) => {
        this.setData({
          kinds: res.data.data.styles
        });
        // 请求分类成功后请求全部数据
        let arr = []
        this.data.kinds.forEach((item) => {
          wx.request({
            url: 'https://apis.netstart.cn/bcomic/ClassPage',
            data: {
              "styleId": item.id
            },
            success: (res) => {
              let o = {
                id: item.id,
                data: res.data.data
              }
              arr.push(o)
              this.setData({
                allKindsData: arr
              })


            }
          })

        });




      }
    })
    //推荐banner

    //2.3.1推荐banner
    wx.request({
      url: 'https://apis.netstart.cn/bcomic/Banner',
      success: (res) => {
        console.log(res);
        console.log("请求离开");
        var bn = res.data.data.map(function (item) {
          let id = Number(item.jump_value.substr(19, 5))
          return {
            ...item,
            comic_id: id
          }
        });
        this.setData({
          bannerData: bn
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("33333333");
    if (this.data.inputValue) {
      this.setData({
        flag: false,
        hisflag: false,
        resultflag: true
      })
      console.log("显示1");


    } else {
      console.log("显示2");
      this.setData({
        flag: true,
        hisflag: false,
        resultflag: false
      })

    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("隐藏了");

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.flag) {
      wx.request({
        url: 'https://apis.netstart.cn/bcomic/Search',
        data: {
          'styleId': -1,
          'areaId': -1,
          'isFinish': -1,
          'order': -1,
          'pageNum': this.data.searchNum++,
          'pageSize': 20,
          'isFree': -1,
          'keyWord': this.data.inputValue
        },
        success: (res) => {
          this.setData({
            resList: this.data.resList.concat(res.data.data.list),
            flag: true
          })

        }
      })
    } else {
      console.log("走22222");
      wx.request({
        url: 'https://apis.netstart.cn/bcomic/ClassPage',
        data: {
          "pageNum": this.data.currentPage++
        },
        success: (res) => {
          this.setData({
            currentData: this.data.currentData.concat(res.data.data)
          })

        }
      })

    }


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})